<?php

namespace App\Admin\Forms;

use App\Models\TongUser;
use App\Models\TongUserAreaLog;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;

class SetUserArea extends Form
{
    use LazyWidget;
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
         //dd($input);
        $area_data = [
            'province_id'=>'',
            'city_id'=>'',
            'area_id'=>'',
            'u_id'=>$input['id'],
            'identity_id'=>$input['identity_id']
        ];
        if(checkIsHasField($input,'province_id')){
            $area_data['province_id'] = $input['province_id'];
        }
        if(checkIsHasField($input,'city_id')){
            $area_data['city_id'] = $input['city_id'];
        }
        if(checkIsHasField($input,'area_id')){
            $area_data['area_id'] = $input['area_id'];
        }
        //查询有没有相同身份甚至了相同地址
        $check_area_query = TongUserAreaLog::query()->where('identity_id',$input['identity_id'])
            ->where('province_id',$input['province_id'])
            ->where('city_id',$input['city_id']);
        if($input['identity_id']==5){
            $check_area_query = $check_area_query->where('area_id',$input['area_id']);
        }
        if($check_area_query->exists()){
            return $this
                ->response()
                ->error('该区域已经设置地址，请勿重复设置')
                ->refresh();
        }
        DB::transaction(function () use($input,$area_data) {
        // return $this->response()->error('Your error message.');
        //TongUser::query()->where('id',$input['id'])->update($area_data);
            $user_area_query = TongUserAreaLog::query()->where('u_id',$input['id'])->where('identity_id',$input['identity_id']);
            if($user_area_query->exists()){
                $user_area_query->update($area_data);
            }else{
                $user_area_query->create($area_data);
            }
        });
        return $this
				->response()
				->success('区域设置成功')
				->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->hidden('id','id')->required();
        $this->hidden('identity_id','identity_id')->required();
        //if($this->default())
        $all_params = $this->default();
        if($all_params['identity_id']==6 || $all_params['identity_id']==7){
            $this->distpicker(['province_id', 'city_id'])->rules('required', [
                'required' => '请选择完整地区']);
        }else{
            $this->distpicker(['province_id', 'city_id','area_id'])->rules('required', [
                'required' => '请选择完整地区']);
        }
        //$this->distpicker(['province_id', 'city_id']);
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [
            'id'=>$this->payload['id'] ?? null,
            'identity_id'=>$this->payload['identity_id'] ?? null,
        ];
    }
}
